/*
 * #%L
 * Server
 * %%
 * Copyright (C) 2011 Ractoc.com
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package com.ractoc.opengamefinder.server;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * The session object is generated for each client which connects to the server.
 * It is present for as long as the client stays connected.
 * 
 * @author ractoc
 * @version 0.1
 */
public final class SessionObject implements Serializable {

    // CHECKSTYLE:OFF
    public static final String PLAYER_ID = "PLAYER_ID";
    // CHECKSTYLE:ON

    private int sessionId;

    private Map<String, Object> sessionParameters = new ConcurrentHashMap<String, Object>();

    private Date lastActivityTime = new Date();

    /**
     * Default constructor.
     */
    public SessionObject() {
    }

    /**
     * Stores a session variable in the session object.
     * 
     * @param key
     *            The key which identifies the variable.
     * @param value
     *            The value of the variable.
     */
    public void setVariable(final String key, final Object value) {
        sessionParameters.put(key, value);
    }

    /**
     * Retreives a stored session variable from the session object.
     * 
     * @param key
     *            The key which identifies the variable.
     * @return The value of the variable.
     */
    public Object getVariable(final String key) {
        return sessionParameters.get(key);
    }

    /**
     * Returns the last time of the last activity of the client. Each time the
     * client send a message to the server, this activity time is updated.
     * 
     * @return The latest activity time of the client.
     */
    public Date getLastActivityTime() {
        return lastActivityTime;
    }

    /**
     * Sets the last time of the last activity of the client. Each time the
     * client send a message to the server, this activity time is updated.
     * 
     * @param lastActivityTimeParam The latest activity time of the client.
     */
    public void setLastActivityTime(final Date lastActivityTimeParam) {
        this.lastActivityTime = lastActivityTimeParam;
    }

    /**
     * Resets the last time of the last activity of the client. Each time the
     * client send a message to the server, this activity time is updated.
     */
    public void resetSessionTimeout() {
        setLastActivityTime(new Date());
    }

    /**
     * Returns the session id of the current session object.
     * @return The session id of the current session object.
     */
    public int getSessionId() {
        return sessionId;
    }

    /**
     * Sets the session id of the current session object.
     * @param sessionIdParam The session id of the current session object.
     */
    public void setSessionId(final int sessionIdParam) {
        this.sessionId = sessionIdParam;
    }

}
